package bsf.baseservice.businessmq.dal;

import bsf.baseservice.businessmq.model.tb_mqpath_partition_model;
import bsf.baseservice.businessmq.systemruntime.EnumMqPathPartitionState;
import bsf.db.DbConn;
import bsf.util.ConvertUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class tb_mqpath_partition_dal
{
	public ArrayList<tb_mqpath_partition_model> getList(DbConn PubConn, int mqpathid)
	{
			java.util.ArrayList<tb_mqpath_partition_model> rs = new java.util.ArrayList<tb_mqpath_partition_model>();
			StringBuilder stringSql = new StringBuilder();
			stringSql.append("select s.* from tb_mqpath_partition s WITH(NOLOCK) where mqpathid=? and state=? order by partitionindex");
		 	List<Map<String,Object>> ds = PubConn.executeList( stringSql.toString(),
					new Object[]{mqpathid, EnumMqPathPartitionState.Running.Value});
			if (ds != null && ds.size() > 0)
			{
				for (Map<String,Object> dr : ds)
				{
					rs.add(createModel(dr));
				}
			}
			return rs;
	}

	public tb_mqpath_partition_model getOfProducter(DbConn PubConn, int partitionindex, int mqpathid)
	{
			StringBuilder stringSql = new StringBuilder();
			stringSql.append("select s.* from tb_mqpath_partition s WITH(NOLOCK) " +
					"where s.partitionindex=? and mqpathid=? and state=?");
		    List<Map<String,Object>> ds =	PubConn.executeList( stringSql.toString(),
					new Object[]{partitionindex,mqpathid, EnumMqPathPartitionState.Running.Value});
			if (ds != null && ds.size() > 00)
			{
				return createModel(ds.get(0));
			}
			return null;
	}

	public tb_mqpath_partition_model getOfConsumer(DbConn PubConn, int partitionindex, int mqpathid)
	{
			StringBuilder stringSql = new StringBuilder();
			stringSql.append("select s.* from tb_mqpath_partition s WITH(NOLOCK) where s.partitionindex=? and mqpathid=?");
			List<Map<String,Object>> ds = PubConn.executeList(stringSql.toString(),
					new Object[]{partitionindex,mqpathid});
			if (ds != null && ds.size() > 0)
			{
				return createModel(ds.get(0));
			}
			return null;
	}

	public tb_mqpath_partition_model createModel(Map<String,Object> dr)
	{
		tb_mqpath_partition_model o = new tb_mqpath_partition_model();

		//
		if (dr.containsKey("id"))
		{
			o.id= ConvertUtil.objToInt(dr.get("id"));
		}
		//ĳ·���µ�mq��id
		if (dr.containsKey("mqpathid"))
		{
			o.mqpathid= ConvertUtil.objToInt(dr.get("mqpathid"));
		}
		//����id���
		if (dr.containsKey("partitionid"))
		{
			o.partitionid= ConvertUtil.objToInt(dr.get("partitionid"));
		}
		//����˳���(ĳ��·����mq��˳���)
		if (dr.containsKey("partitionindex"))
		{
			o.partitionindex= ConvertUtil.objToInt(dr.get("partitionindex"));
		}
		//ĳ·����mq��״̬,1 �����У�0 ������Ǩ�ƻ�ֹͣ��-1 ��ɾ��
		if (dr.containsKey("state"))
		{
			o.state= ConvertUtil.objToByte(dr.get("state"));
		}
		//����ʱ��(�Ե�ǰ��ʱ��Ϊ׼)
		if (dr.containsKey("createtime"))
		{
			o.createtime= ConvertUtil.objToDateTime(dr.get("createtime"));
		}
		return o;
	}
}